//
//  ViewController.m
//  TommaoUIImageView
//
//  Created by yb on 16/3/10.
//  Copyright © 2016年 yb. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *ImageViewCat;

- (IBAction)eat;
- (IBAction)cymbal;

- (IBAction)knockout;
- (IBAction)drink;
- (IBAction)pie;
- (IBAction)fart;
- (IBAction)scratch;
- (IBAction)stomach;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)startAnimating:(int)count picName:(NSString*)picName
{
    //判断当前动画是否正在执行，如果正在执行该点击无效
    if (self.ImageViewCat.isAnimating == 1)
    {
        return;
    }
    
    //1.加载图片到帧动画数组中去
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i = 0; i<count; i++) {
        //拼接图片名字
        NSString *imageName = [NSString stringWithFormat:@"%@_%02d.jpg",picName,i];
        //根据图片文字得到图片
        UIImage *imgCat = [UIImage imageNamed:imageName];
        
        //NSString *path = [[NSBundle mainBundle]pathForResource:imageName ofType:nil];
        //UIImage *imgCat = [UIImage imageWithContentsOfFile:path];
        
        //将每次获取的图片放到数组中去
        [arrayM addObject:imgCat];
    }
    //2.将得到的帧动画数组赋给UIImageView的帧动画数组属性
    self.ImageViewCat.animationImages = arrayM;
    
    //3.设置动画时间
    self.ImageViewCat.animationDuration = self.ImageViewCat.animationImages.count*0.1;
    
    //4.设置动画循环次数
    self.ImageViewCat.animationRepeatCount = 1;
    
    //5.开始播放动画
    [self.ImageViewCat startAnimating];
    
    //6.动画播放完清空缓存
    //self.ImageViewCat.animationImages = nil ;
    [self.ImageViewCat performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.ImageViewCat.animationImages.count *0.1];
}

- (IBAction)eat
{
    [self startAnimating:34 picName:@"eat"];
}

- (IBAction)cymbal
{
    [self startAnimating:12 picName:@"cymbal"];
}

- (IBAction)knockout
{
    [self startAnimating:80 picName:@"knockout"];
}

- (IBAction)drink
{
    [self startAnimating:80 picName:@"drink"];
}

- (IBAction)pie {
    [self startAnimating:23 picName:@"pie"];

}

- (IBAction)fart {
    [self startAnimating:27 picName:@"fart"];

}

- (IBAction)scratch {
    [self startAnimating:55 picName:@"scratch"];

}

- (IBAction)stomach {
    [self startAnimating:33 picName:@"stomach"];

}








@end
